<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
	#div1 {width: 100px; height: 100px; background: red; position: absolute;}
	.box {border: 1px dashed black;position: absolute;}
</style>
<script>
window.onload = function()
{
		var oDiv = document.getElementById('div1');

	
		var disX =0 ;
		var disY = 0;
		document.oncontextmenu=function(ev)
		{

				return false;
		}
		oDiv.onmousedown= function(ev)
		{
				//clientX clientY 鼠标坐标
				var oEvent = ev||event;
				disX = oEvent.clientX- oDiv.offsetLeft ;
				disY = oEvent.clientY -oDiv.offsetTop ;
			
				var oBox = document.createElement('div');
			
				oBox.className = 'box';
				oBox.style.width = oDiv.offsetWidth +'px';
				oBox.style.height = oDiv.offsetHeight +'px';
			
				oBox.style.width = oDiv.offsetWidth-2 +'px';   // 因为border 占了1， 左右个1px
				oBox.style.height = oDiv.offsetHeight-2 +'px';
			
				oBox.style.left = oDiv.offsetLeft +'px';
				oBox.style.top = oDiv.offsetTop +'px';
			
				document.body.appendChild(oBox);

				document.onmousemove= function(ev)
				{
						var oEvent = ev||event;
						var left = oEvent.clientX - disX;
						var top = oEvent.clientY - disY;
						var rang = 50;
						
						if(left<rang)
						{
								left = 0;
						}
					
						if(top<rang)
						{
								top = 0;
						}
				
					
						oBox.style.left = left + 'px';
						oBox.style.top = top + 'px';
						
				};
				
			document.onmouseup = function()
			{
					document.onmousemove =null;
					document.onmouseup=null;
				
					oDiv.style.left = oBox.offsetLeft +'px';
				 oDiv.style.top = oBox.offsetTop + 'px';
					document.body.removeChild(oBox);
			};
			
		};
};
</script>
</head>

<body>
	<div id="div1"></div>
</body>
</html>
